﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_InsertPet (smp_insert_favor)
    /// </summary>
    public class DB_InsertPet : IDBProc
    {
        Pet m_pPet;
        int sid;
        int account_id;
        int owner_id;
        long cage_uid;
        int code;
        bool name_changed;
        string name;

        public BaseObject m_CBObject = null;

        public DB_InsertPet(Pet pPet)
        {
            this.m_pPet = pPet;
            this.sid = pPet.m_nSID;

            if (pPet.m_item != null)
            {
                this.account_id = pPet.m_item.m_nAccountID;
                this.cage_uid = pPet.m_item.m_Instance.UID;
            }
            else
            {
                this.account_id = 0;
                this.cage_uid = 0;
            }

            if (pPet.m_master != null)
                this.owner_id = pPet.m_master.m_nUID;
            else
                this.owner_id = 0;

            this.code = pPet.m_ContentInfo.uid;
            this.name_changed = pPet.m_bNameChanged;
            this.name = pPet.GetName();
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_insert_pet";
            cmd.CommandType = CommandType.StoredProcedure;

            DbParameter IN_SID = GameDBManager.Instance.CreateInParameter(cmd, "IN_SID", DbType.Int32, this.sid);
            DbParameter IN_ACCOUNT_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_ACCOUNT_ID", DbType.Int32, this.account_id);
            DbParameter IN_OWNER_ID = GameDBManager.Instance.CreateInParameter(cmd, "IN_OWNER_ID", DbType.Int32, this.owner_id);
            DbParameter IN_CAGE_UID = GameDBManager.Instance.CreateInParameter(cmd, "IN_CAGE_UID", DbType.Int64, this.cage_uid);
            DbParameter IN_CODE = GameDBManager.Instance.CreateInParameter(cmd, "IN_CODE", DbType.Int32, this.code);
            DbParameter IN_NAME_CHANGED = GameDBManager.Instance.CreateInParameter(cmd, "IN_NAME_CHANGED", DbType.Int32, this.name_changed ? 1 : 0);
            DbParameter IN_NAME = GameDBManager.Instance.CreateInParameter(cmd, "IN_NAME", DbType.String, this.name);

            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_InsertSummon.onProcess Exception: {0}", ex);
            }
            conn.Close();

            return true;
        }
    }


}

